package com.zeyi.blog.social.github;

import com.zeyi.blog.entity.User;
import com.zeyi.blog.service.UserService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

/**
 * @author 黎先森
 */
public class GithubAuthProvider implements AuthenticationProvider {

    private UserService userService;

    public GithubAuthProvider(UserService userService) {
        this.userService = userService;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        GithubAuthToken authToken = (GithubAuthToken) authentication;
        GithubUser githubUser = (GithubUser) authToken.getPrincipal();
        User user = userService.thirdLogin(githubUser);
        return new GithubAuthToken(user);
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return GithubAuthToken.class.isAssignableFrom(aClass);
    }
}
